package com.hunktimes.wholesale.ui.pages

import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayoutMediator
import com.hunktimes.wholesale.R
import com.hunktimes.wholesale.base.BasePageFragment
import com.hunktimes.wholesale.databinding.TitleTabViewpagerV2Binding
import com.hunktimes.wholesale.util.Constants
import com.hunktimes.wholesale.widget.SmartRefreshDelegate
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * Author: Delete
 * Date: 2020/7/27 19:22
 * Description:
 *
 */
class MyOrderV2Fragment : BasePageFragment(R.layout.title_tab_viewpager_v2),
    OnRefreshLoadMoreListener {
    lateinit var viewBinding: TitleTabViewpagerV2Binding
    override val titleRes: Int
        get() = R.string.title_order

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewBinding = TitleTabViewpagerV2Binding.bind(view)
    }

    override fun onStart() {
        super.onStart()
        initTabLayout()
    }

    override fun onResume() {
        super.onResume()
        selectSpecificTabDelay()
    }

    private fun selectSpecificTabDelay() {
        arguments?.getInt(Constants.key_selected_tab)?.let {
            if (it < 0 || it >= viewBinding.tabLayout.tabCount) return
            lifecycleScope.launch {
                delay(500)//不延迟的话 viewPager不联动
                viewBinding.tabLayout.selectTab(viewBinding.tabLayout.getTabAt(it))
            }
        }
    }

    val titles = arrayOf("全部", "待付款", "待發貨", "待收貨", "待評價")
    fun initTabLayout() {
        viewBinding.srl.setOnRefreshLoadMoreListener(this)
        val fragments = titles.map { OrderListV2Fragment.newInstance() }
        viewBinding.viewPager.adapter = OrderTabAdapter(this, fragments)

        TabLayoutMediator(viewBinding.tabLayout, viewBinding.viewPager) { tab, position ->
            tab.setCustomView(R.layout.tab_view_simple)
            tab.customView!!.findViewById<TextView>(R.id.tab_text).text = titles[position]
            tab.view.setBackgroundColor(
                ContextCompat.getColor(
                    requireContext(),
                    android.R.color.transparent
                )
            )
        }.attach()
    }

    inner class OrderTabAdapter(fragment: Fragment, val fragments: List<OrderListV2Fragment>) :
        FragmentStateAdapter(fragment) {


        override fun getItemCount(): Int = fragments.size

        override fun createFragment(position: Int): Fragment {
            val bundle = bundleOf(Constants.key_order_tab_type to position)
            val fragment = fragments.get(position)
            fragment.arguments = bundle
            return fragment
        }

    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        (viewBinding.viewPager.adapter as OrderTabAdapter).fragments[viewBinding.viewPager.currentItem].onRefresh()
        viewBinding.srl.postDelayed({ viewBinding.srl.finishRefresh() }, 2000)
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        (viewBinding.viewPager.adapter as OrderTabAdapter).fragments[viewBinding.viewPager.currentItem].onLoadMore()
        viewBinding.srl.postDelayed({ viewBinding.srl.finishLoadMore() }, 2000)
    }
}